﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StoreTransport.DAO;
using StoreTransport.Business.Helper;
using StoreTransport.Business.Constants;
using StoreTransport.Resources;

namespace StoreTransport.Presentation.Forms {
    public partial class ImportProductsForm : Form {
        public ImportProductsForm() {
            InitializeComponent();
        }

        private void _btxBrowseFile_Click(object sender, EventArgs e) {
            var FD = new System.Windows.Forms.OpenFileDialog();
            if (FD.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                _tbxImportFilePath.Text = FD.FileName;
            }
        }

        private void _btxImportData_Click(object sender, EventArgs e) {
            Cursor.Current = Cursors.WaitCursor;
            string errorMessage = CommonHelper.CheckRequiredField(_tbxImportFilePath.Text, _lblImportFilePath.Text);
            if (!string.IsNullOrEmpty(errorMessage)) {
                MessageBox.Show(errorMessage);
                return;
            }
            if (!(_tbxImportFilePath.Text.ToLower().EndsWith(CommonConstant.ExcelFileExtendtion.ToLower()))) {
                errorMessage = Resources.StoreTransportResource.ErrorMessage_FilePathNotValid;
                MessageBox.Show(errorMessage);
                return;
            }
            _lblWaitingBox.Visible = true;
            if (_drpProductLine.SelectedIndex != -1) {
                StoreTransportDataContext context = CommonHelper.GetContext();
                context.SubmitChanges();
                ExcelHelper.ImportProducts(_tbxImportFilePath.Text, int.Parse(_drpProductLine.SelectedValue.ToString()));
            } else {
                _lblWaitingBox.Visible = false;
                MessageBox.Show(StoreTransportResource.MissingProductLine);
                return;
            }
            _lblWaitingBox.Visible = false;
            Cursor.Current = Cursors.Default;
            MessageBox.Show(StoreTransportResource.ImportFinishes);
        }

        private void ImportProduct_Load(object sender, EventArgs e) {
            _drpProductLine.DisplayMember = ColumnName.ProductLineName;
            _drpProductLine.ValueMember = ColumnName.ID;
            _drpProductLine.DataSource = CommonHelper.GetContext().ProductLines;
            _drpProductLine.Select();
        }
    }
}
